package com.itany.nmms.service.impl;

import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.StaffDao;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.StaffService;
import com.itany.nmms.util.MD5Util;
import com.itany.nmms.util.ParameterUtil;

/**
 * @author 石小俊
 * @date 2023年07月21日 14:58
 */
public class StaffServiceImpl implements StaffService {
    @Override
    public Staff login(String loginName, String password, String role, String code, String image) throws RequestParameterErrorException, CodeErrorException, StaffNotExistException {
        StaffDao staffDao = (StaffDao) ObjectFactory.getObject("staffDao");
        if(ParameterUtil.isNull(loginName) || ParameterUtil.isNull(password) || ParameterUtil.isNull(role) || ParameterUtil.isNull(code) || ParameterUtil.isNull(image)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        if(!code.equals(image)){
            throw new CodeErrorException("验证码不正确");
        }

        // 密码必须加密,员工必须在职
        Staff staff = staffDao.selectByLoginParam(loginName, MD5Util.md5(password),role, StatusConstant.STAFF_IS_VALID_YES);
        if(staff == null){
            throw new StaffNotExistException("账号或密码错误");
        }
        return staff;
    }
}
